<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/7/23
 * Time: 13:38
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Question extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['question_name'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($status)&&$status!="")
            {
                $map['question_type'] = $status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('question')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('question')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('question_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?question_id')){
            session('question_id',null);
        }
        return $this->fetch();
    }

    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $count = Db::table('up_question_choice')->where('question_id',$task_id)->count();
        if($count>0){
            Db::table('up_question_choice')->where('question_id',$task_id)->delete();
        }
        $res = Db::name('question')->where('question_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $report_type_name=input('report_type_name');
            $question_type=input('status');
            $data['question_name'] = $report_type_name;
            $data['question_type'] = $question_type;
            $data['question_time'] = date('Y-m-d H:i:s',time());
            $data['course_id'] = input('course_id');
            $res = Db::table('up_question')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $info = Db::table('up_course')->field('course_id,course_name')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('param.id');
            $report_type_name=input('report_type_name');
            $data['question_name'] = $report_type_name;
            $data['course_id'] = input('course_id');
            $res = Db::table('up_question')->where('question_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $id = input('question_id');
        $info= Db::table('up_question')->where('question_id',$id)->find();
        $info1 = Db::table('up_course')->field('course_id,course_name')->select();
        $this->assign('info',$info1);
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }


    public function block(){
        if(request()->isAjax()){
            extract(input());
            $map['question_id'] = session('question_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('question_choice')->alias('a')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('question_choice')->alias('a')
                ->where($map)
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(!session('?question_id')){
            session('question_id',input('question_id'));
        }
        return $this->fetch();
    }

    public function add1(){
        if(Request::instance()->isPost()){
            $report_type_name=input('report_type_name');
            $question_type=input('status');
            $data['choice_name'] = $report_type_name;
            $data['choice_type'] = $question_type;
            $data['question_id'] =  session('question_id');
            $data['choice_time'] = date('Y-m-d H:i:s',time());
            $q_type = Db::table('up_question')
                ->where('question_id',session('question_id'))
                ->value('question_type');
            if($q_type == 1 && $question_type == 1){
                $have = Db::table('up_question_choice')
                    ->where('question_id',session('question_id'))
                    ->where('choice_type',1)
                    ->find();
                if($have){
                    return json(['code' => 100, 'msg' => '已经有正确答案了']);
                }
            }
            $res = Db::table('up_question_choice')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del1(){
        $task_id = input('param.id');
        $res = Db::name('up_question_choice')->where('choice_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    public function update1(){

        if(Request::instance()->isPost()){
            $id = input('param.id');
            $report_type_name=input('report_type_name');
            $question_type=input('status');
            $data['choice_name'] = $report_type_name;
            $data['choice_type'] = $question_type;
            //
            $old = Db::table('up_question_choice')->where('choice_id',$id)->find();
            $question_id = $old['question_id'];
            $q_type = Db::table('up_question')
                ->where('question_id',$question_id)
                ->value('question_type');
            if($q_type == 1 && $question_type == 1){
                $have = Db::table('up_question_choice')
                    ->where('question_id',$question_id)
                    ->where('choice_id','<>',$id)
                    ->where('choice_type',1)
                    ->find();
                if($have){
                    return json(['code' => 100, 'msg' => '已经有正确答案了']);
                }
            }
            $res = Db::table('up_question_choice')->where('choice_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $id = input('choice_id');
        $info= Db::table('up_question_choice')->where('choice_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}